package org.mx.dal.entity;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.Getter;
import lombok.Setter;

/**
 * Geo Multi Line String定义类
 * 
 * @author john.peng
 *
 */
@Getter
@Setter
public class GeoShapeMultiLineString extends GeoShape {
	private static final Logger logger = LoggerFactory.getLogger(GeoShapeMultiLineString.class);

	@JsonIgnore
	private List<GeoShapeLineString> lines = new ArrayList<>();

	/**
	 * 默认的构造函数
	 */
	public GeoShapeMultiLineString() {
		super();
		super.setType("MultiLineString");
	}

	private void fillData() {
		if (lines.isEmpty()) {
			if (logger.isWarnEnabled()) {
				logger.warn("The Multi line string is empty.");
			}
			super.setCoordinates(new double[0][0]);
		} else {
			double[][][] coordinates = new double[lines.size()][][];
			for (int index = 0; index < lines.size(); index++) {
				double[][] line = new double[lines.get(index).getPoints().size()][2];
				for (int point = 0; point < lines.get(index).getPoints().size(); point++) {
					line[point][0] = lines.get(index).getPoints().get(point).getLon();
					line[point][1] = lines.get(index).getPoints().get(point).getLat();
					coordinates[index] = line;
				}
			}
			super.setCoordinates(coordinates);
		}
	}

	/**
	 * 向Multi Line中添加一个点
	 * @param line 点
	 * @return Multi Line String
	 */
	public GeoShapeMultiLineString addLine(GeoShapeLineString line) {
		lines.add(line);
		fillData();
		return this;
	}

	/**
	 * 向Multi Line中添加几个点
	 * @param lines 点集合
	 * @return Multi Line String
	 */
	public GeoShapeMultiLineString addLines(List<GeoShapeLineString> lines) {
		this.lines.addAll(lines);
		fillData();
		return this;
	}

	/**
	 * 清除Multi Line的点
	 * @return Multi Line String
	 */
	public GeoShapeMultiLineString clear() {
		lines.clear();
		fillData();
		return this;
	}
}
